package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryStructureBo;
import org.eiahe.hr.salary.domain.vo.SalaryStructureVo;
import org.eiahe.hr.salary.service.ISalaryStructureService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 薪资构成Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/structure")
public class SalaryStructureController extends BaseController {

    private final ISalaryStructureService iSalaryStructureService;

    /**
     * 查询薪资构成列表
     */
    @SaCheckPermission("salary:structure:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryStructureVo> list(SalaryStructureBo bo, PageQuery pageQuery) {
        return iSalaryStructureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出薪资构成列表
     */
    @SaCheckPermission("salary:structure:export")
    @Log(title = "薪资构成", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryStructureBo bo, HttpServletResponse response) {
        List<SalaryStructureVo> list = iSalaryStructureService.queryList(bo);
        ExcelUtil.exportExcel(list, "薪资构成数据", SalaryStructureVo.class, response);
    }

    /**
     * 获取薪资构成详细信息
     *
     * @param structureId 主键
     */
    @SaCheckPermission("salary:structure:query")
    @GetMapping("/{structureId}")
    public R<SalaryStructureVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("structureId") Long structureId) {
        return R.ok(iSalaryStructureService.queryById(structureId));
    }

    /**
     * 新增薪资构成
     */
    @SaCheckPermission("salary:structure:add")
    @Log(title = "薪资构成", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryStructureBo bo) {
        return toAjax(iSalaryStructureService.insertByBo(bo));
    }

    /**
     * 修改薪资构成
     */
    @SaCheckPermission("salary:structure:edit")
    @Log(title = "薪资构成", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryStructureBo bo) {
        return toAjax(iSalaryStructureService.updateByBo(bo));
    }

    /**
     * 删除薪资构成
     *
     * @param structureIds 主键串
     */
    @SaCheckPermission("salary:structure:remove")
    @Log(title = "薪资构成", businessType = BusinessType.DELETE)
    @DeleteMapping("/{structureIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] structureIds) {
        return toAjax(iSalaryStructureService.deleteWithValidByIds(Arrays.asList(structureIds)));
    }
}